package com.app.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.HashMap;
import java.util.List;

import com.app.Task;

public class BroadcastListener extends Task {

	private DatagramSocket socket;
	private DatagramPacket packet;
	private HashMap<InetAddress, String> hosts = new HashMap<InetAddress, String>(); 
	
	public BroadcastListener(int port) {
		try {
			socket = new DatagramSocket(port);
			socket.setBroadcast(true);
		} catch (SocketException e) {
			e.printStackTrace();
		}
		
		packet = new DatagramPacket(new byte[40], 40);
	}
	@Override
	protected void task() {
		try {
			socket.receive(packet);
			String data = new String(packet.getData());
			hosts.put(packet.getAddress(), data);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
